import _ from 'lodash';
import { server } from 'configs';
import previewImg from 'components/common/ImgPreview';
/**
 * 共享工具方法
 * 2018-06-21
 */
const defultKey = '-';

/**
 * 数组内查询
 * @param   {array}      array
 * @param   {String}    id
 * @param   {String}    keyAlias
 * @return  {Array}
 */
export const queryArray = (array, key, keyAlias = 'key') => {
  if (!(array instanceof Array)) {
    return null;
  }
  const item = array.filter(_ => _[keyAlias] === key);
  if (item.length) {
    return item[0];
  }
  return null;
};

export const formatValue = value => {
  if (value !== '' && !_.isNil(value)) {
    return value;
  }
  return defultKey;
};

/**
 *
 * @param {*} array
 */
export const getRealArray = () => {
  const result = [];
  return function deep (array, dp) {
    if (result.length > 0 && !dp) return result;
    for (let i = 0; i < array.length; i += 1) {
      result.push(array[i]);
      if (array[i].children && array[i].children.length > 0) {
        deep(array[i].children, true);
      }
    }

    return result;
  };
};
const cacheArray = getRealArray();
export const queryDeepArray = (array, key, keyAlias) => {
  const searchArray = cacheArray(array);
  return queryArray(searchArray, key, keyAlias);
};

export const getServer = ()  =>{
  const DEPLOY_ENV = process.env.DEPLOY_ENV || 'dev';
  return server[DEPLOY_ENV];
};
export const extraFields = (fields, extra) => {
  const fieldsObj = fields.map(item => {
    const exIndex = extra.findIndex(ex => ex.dataIndex === item.dataIndex);
    if(exIndex !== -1){
      const exItem = extra[exIndex];
      extra.splice(exIndex, 1);
      return exItem;
    }
    return item;
  });
  return fieldsObj.concat(extra);
};

/**
 * 判断是否是对象
 * @param {*} obj obj
 */
export const isObject = (obj) => {
  return obj !== null && typeof obj === 'object';
};

/**
 *
 * @param {*} a 对象深比较A
 * @param {*} b 对象深比较A
 */
export const looseEqual = (a, b) => {
  if (a === b) return true;
  const isObjectA = isObject(a);
  const isObjectB = isObject(b);
  if (isObjectA && isObjectB) {
    try {
      const isArrayA = Array.isArray(a);
      const isArrayB = Array.isArray(b);
      if (isArrayA && isArrayB) {
        return a.length === b.length && a.every((e, i) => looseEqual(e, b[i]));
      } else if (!isArrayA && !isArrayB) {
        const keysA = Object.keys(a);
        const keysB = Object.keys(b);
        return keysA.length === keysB.length && keysA.every(key => looseEqual(a[key], b[key]));
      }
      /* istanbul ignore next */
      return false;
    } catch (e) {
      /* istanbul ignore next */
      return false;
    }
  } else if (!isObjectA && !isObjectB) {
    return String(a) === String(b);
  } else {
    return false;
  }
};
// undefined,null,空字符串
export const isEmpty = value => _.isNil(value) || (_.isString(value) && _.isEmpty(value));
// 时间转换
export const combineDate = (dateTime = [], format) => {
  let beginTime = '';
  let endTime = '';
  if (dateTime.length === 2) {
    const startStr = !format ? `${dateTime[0].format('YYYY-MM-DD')} 00:00:00` : `${dateTime[0].format(format)}`;
    const endStr = !format ? `${dateTime[1].format('YYYY-MM-DD')} 23:59:59` : `${dateTime[1].format(format)}`;

    beginTime = startStr;
    endTime = endStr;
  }
  return { beginTime, endTime };
};

// 对象转key
export const OTOKEY = (val) => {
  const obj = {};
  Object.keys(val).forEach(el => {
    obj[val[el].key] = el;
  });
  return obj;
};
export const getValueFromObject = (datas, keyStr) => {
  if (!keyStr) {
    return '';
  }
  const keys = keyStr.split('.');
  return keys.reduce((findDatas, key) => (findDatas || {})[key], datas || {});
};
export const renderImg = imgUrl => imgUrl
  ? <div className="imgItem">
    <a onClick={() => { previewImg({ imgUrl }); }} >
      <img src={imgUrl} alt="图片加载失败" className="avatarImg" />
    </a>
  </div>
  : '-';